﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;

namespace AdminUI.Pages.OrderManage
{
    /// <summary>
    /// 付款审核
    /// </summary>
    public partial class Order20ConfirmModel : BaseModel
    {
        public void OnGet(long id)
        {
            Current = OrderBO.Instance.GetItem<Order>(id);

            var list = TenantAccountBO.Instance.GetList(OnlineTID);
            var items = new List<NameValueDto> { new NameValueDto { Name = "请选择收款账户", Value = string.Empty } };
            foreach (var item in list)
            {
                if (item.AccountNo.IsNullOrEmpty() || item.AccountNo.Length < 5) continue;
                var len = item.AccountNo.Length;
                items.Add(new NameValueDto { Name = $"{item.BankName}{item.AccountNo.Remove(0, len - 4)}", Value = $"{item.BankName}({item.AccountName}-{item.AccountNo})" });
            }
            ViewBag.AccountList = items;

            var userList = UserBO.Instance.GetListByRole(OnlineTID, 1);
            ViewBag.UserList = userList;
        }

        public Order Current { get; set; }

        public IActionResult OnPostBtnSubmit_Click(long orderID, long auditorID, DateTime transferDate, string payeeAccount, string tradeNo)
        {
            try
            {
                OrderBO.Instance.PaymentAuditing(auditorID, orderID, payeeAccount, transferDate, tradeNo);
                LogAction(ActionType.确认, $"付款审核：{orderID}");

                FineUICore.PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                //ActiveWindow.HidePostBack();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }

    }
}